package cn.iocoder.yudao.module.pypm.service.projectmember;

import jakarta.annotation.Resource;

import org.springframework.stereotype.Service;

import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.pypm.controller.admin.projectmember.vo.*;
import cn.iocoder.yudao.module.pypm.dal.dataobject.projectmember.ProjectMemberDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.pypm.dal.mysql.projectmember.ProjectMemberMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.pypm.enums.ErrorCodeConstants.*;

/**
 * 项目成员中间 Service 实现类
 *
 * @author 钦州码头
 */
@Service
@Validated
public class ProjectMemberServiceImpl implements ProjectMemberService {

    @Resource
    private ProjectMemberMapper projectMemberMapper;

    @Override
    public Long createProjectMember(ProjectMemberSaveReqVO createReqVO) {
        // 插入
        ProjectMemberDO projectMember = BeanUtils.toBean(createReqVO, ProjectMemberDO.class);
        projectMemberMapper.insert(projectMember);
        // 返回
        return projectMember.getId();
    }

    @Override
    public void updateProjectMember(ProjectMemberSaveReqVO updateReqVO) {
        // 校验存在
        validateProjectMemberExists(updateReqVO.getId());
        // 更新
        ProjectMemberDO updateObj = BeanUtils.toBean(updateReqVO, ProjectMemberDO.class);
        projectMemberMapper.updateById(updateObj);
    }

    @Override
    public void deleteProjectMember(Long id) {
        // 校验存在
        validateProjectMemberExists(id);
        // 删除
        projectMemberMapper.deleteById(id);
    }

    private void validateProjectMemberExists(Long id) {
        if (projectMemberMapper.selectById(id) == null) {
            throw exception(PROJECT_MEMBER_NOT_EXISTS);
        }
    }

    @Override
    public ProjectMemberDO getProjectMember(Long id) {
        return projectMemberMapper.selectById(id);
    }

    @Override
    public PageResult<ProjectMemberDO> getProjectMemberPage(ProjectMemberPageReqVO pageReqVO) {
        return projectMemberMapper.selectPage(pageReqVO);
    }

}